package com.whut.transportation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.whut.transportation.adapter.TrainAdapter;
import com.whut.transportation.entity.AllTrainInfo;
import com.whut.transportation.entity.TrainInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;
/**
 * 查询列车信息
 * @author yuliyan
 *
 */
public class SearchTrainActivity extends BaseActivity {
	
	private ListView traininfo;
	private List<TrainInfo> traininfos;
	private TrainAdapter trainAdapter;
	
	private SharedPreferences preferences;
	private Intent intent;
	
	@Override
	protected void onResume() {
		if(trainAdapter == null){
			intent = getIntent();
			getData(false);
		}
		super.onResume();
	}

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add_more:
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	
	@Override
	protected void findViewById() {
		traininfo = (ListView) findViewById(R.id.traininfo);
	}

	@Override
	protected void afterFindViewId() {
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_search_train);
		setTitleBarView(true, "搜索", -1, true);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
	}
	@Override
	protected void processLogic() {
		traininfos = new ArrayList<TrainInfo>();
	}
	@Override
	protected void setListener() {
	}

	@Override
	protected void getDataAgain() {
		
	}
	
	private void getData(boolean b) {
		String trainId = intent.getStringExtra("trainId");
		HttpRequestVo vo = new HttpRequestVo();
		vo.context = mContext;
		if("不限".equals(trainId)){
			vo.requestUrl = Constant.FIND_ALL_TRAIN_INFO;
			vo.requestDataMap = null;
			vo.parser = new JSONParser(AllTrainInfo.class);
			getDataFromServer(HttpManager.GET_MOTHOD, vo, 
					new DataCallback<HashMap<String, Object>>() {
						@Override
						public void onStart() {
							showProgressDialog(getResources().getString(
									R.string.loading_content));
						}
						@Override
						public void onFailed() {
							showNetErrorDialog();
						}
						@Override
						public void processData(
								HashMap<String, Object> paramObject,
								boolean paramBoolean) {
							int code = (Integer) paramObject.get("code");
							if(code == 1){
								AllTrainInfo allTranInfos = (AllTrainInfo) paramObject.get("result");
								traininfos.addAll(allTranInfos.ListTrainInfo);
								trainAdapter = new TrainAdapter(mContext, traininfos, R.layout.activity_train_item);
								traininfo.setAdapter(trainAdapter);
							} else {
								Toast.makeText(mContext, "目前没有数据", Toast.LENGTH_SHORT).show();
							}
						}
						@Override
						public void onFinish() {
							closeProgressDialog();
						}
					});
		} else {
			vo.requestUrl = Constant.FIND_ONE_TRAIN_INFO;
			HashMap<String, String> req = new HashMap<String, String>();
			req.put("trainInfo.trainId", trainId);
			vo.requestDataMap = req;
			vo.parser = new JSONParser(TrainInfo.class);
			getDataFromServer(HttpManager.POST_MOTHOD, vo, 
					new DataCallback<HashMap<String, Object>>() {

						@Override
						public void onStart() {
						}
						@Override
						public void onFailed() {
							showNetErrorDialog();
						}

						@Override
						public void processData(
								HashMap<String, Object> paramObject,
								boolean paramBoolean) {
							TrainInfo trainInfo = (TrainInfo) paramObject.get("result");
							if(trainInfo.getTrainNo()!=0){
								traininfos.add(trainInfo);
								trainAdapter = new TrainAdapter(mContext, traininfos, R.layout.activity_train_item);
								traininfo.setAdapter(trainAdapter);
							} else {
								Toast.makeText(mContext, "目前没有数据", Toast.LENGTH_SHORT).show();
							}
						}
						@Override
						public void onFinish() {
							closeProgressDialog();
						}
					});
		}
		
	}

}
